#include "mainwindow.h"
#include "./ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    resize(800, 600); // 设置窗口大小
    setWindowTitle(QString("登录窗口"));

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addWidget(usernameEdit);
    mainLayout->addWidget(passwordEdit);

    usernameEdit = new QLineEdit(this); // 输入框，绑定父组件
    usernameEdit->move(200, 200);
    usernameEdit->resize(400, 30);
    usernameEdit->setPlaceholderText(QString("请输入用户名"));
    usernameEdit->setToolTip(QString("用户名只能包含英文或数字"));
    QFont font;
    font.setPixelSize(15); // 字号
    usernameEdit->setFont(font);


    passwordEdit = new QLineEdit(this);
    passwordEdit->move(200, 250);
    passwordEdit->resize(400, 30);
    passwordEdit->setFont(font);
    passwordEdit->setPlaceholderText(QString("请输入密码"));
    passwordEdit->setToolTip(QString("密码开头必须为大写字母"));
    passwordEdit->setEchoMode(QLineEdit::Password); // 密码框

    // 复选框
    autoLoginBox = new QCheckBox(this);
    autoLoginBox->move(200, 290);
    autoLoginBox->setText(QString("自动登录"));

    rememberBox = new QCheckBox(this);
    rememberBox->move(530, 290);
    rememberBox->setText(QString("记住密码"));

    // 设置按钮
    registerBut = new QPushButton(this);
    registerBut->setText(QString("注册"));
    registerBut->resize(100, 40);
    registerBut->move(200, 330);

    loginBut = new QPushButton(this);
    loginBut->setText(QString("登录"));
    loginBut->resize(100, 40);
    loginBut->move(500, 330);
    // 按钮样式
    loginBut->setStyleSheet(QString("QPushButton:pressed { background-color: navy; }"));

    // 信号与槽
    connect(loginBut, &QPushButton::clicked, this, &MainWindow::onloginButton);
    connect(registerBut, &QPushButton::clicked, this, &MainWindow::onRegisterButton);
    connect(usernameEdit, &QLineEdit::textChanged, this, &MainWindow::usernameTextChanged);
    connect(passwordEdit, &QLineEdit::textChanged, this, &MainWindow::passwordTextChanged);

    connect(usernameEdit, &QLineEdit::editingFinished, this, &MainWindow::usernameEditEnter);// 输入回车触发
    connect(passwordEdit, &QLineEdit::editingFinished, this, &MainWindow::passwordEditEnter);// 输入回车触发
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onloginButton()
{
    // 检查数据格式

    // 登录

    QMessageBox msgBox;
    msgBox.setWindowTitle((QString("登录信息：")));
    msgBox.setText("登录成功！");
    msgBox.exec();
}

void MainWindow::onRegisterButton()
{
    QMessageBox msgBox;
    msgBox.setWindowTitle((QString("注册信息：")));
    msgBox.setText("我还没写！");
    msgBox.exec();
}

void MainWindow::usernameTextChanged()
{
    QString tmp = QString(usernameEdit->text());
    if (tmp.size() == 0)
        return;

    QChar last = usernameEdit->text().back();
    if ((last >= 'a' && last <= "z") || (last >= 'A' && last <= 'Z') || (last >= '0' && last <= '9'))
        return;

    // 显示提示（不会抢焦点）
    QToolTip::showText(usernameEdit->mapToGlobal(QPoint(0, -30)),  // 在输入框上方显示
                       "用户名只能包含英文或数字",
                       usernameEdit);  // 指定父组件确保跟随移动

    // 隐藏提示
    QToolTip::hideText();

    if (tmp.size() > 0)
        usernameEdit->setText(tmp.left(tmp.size() - 1)); // 恢复到合法的字符串
}

void MainWindow::passwordTextChanged()
{
    if (passwordEdit->text().size() == 0)
        return;

    QChar first = passwordEdit->text().front();
    if (first >= 'A' && first <= 'Z')
        return;

    // 显示提示（不会抢焦点）
    QToolTip::showText(passwordEdit->mapToGlobal(QPoint(0, -30)),  // 在输入框上方显示
                       "密码开头必须为大写字母",
                       passwordEdit);  // 指定父组件确保跟随移动

    // 隐藏提示
    QToolTip::hideText();

    passwordEdit->clear();
}

void MainWindow::usernameEditEnter()
{
    passwordEdit->setFocus();
}

void MainWindow::passwordEditEnter()
{
    loginBut->click();
}


